package com.zbkj.crmeb.upload.controller;

import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import com.zbkj.crmeb.system.service.SystemAttachmentService;
import com.zbkj.crmeb.upload.service.UploadService;
import com.zbkj.crmeb.upload.vo.FileResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;


/**
 * 商品表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/upload")
@Api(tags = "上传文件")

public class UploadController extends AdminBaseController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    /**
     * 图片上传
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "图片上传")
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章"),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public CommonResult<FileResultVo> image(MultipartFile multipart,
                                            @RequestParam(value = "model") String model,
                                            @RequestParam(value = "pid") Integer pid) throws IOException {
        return CommonResult.success(uploadService.image(multipart, model, pid));
    }

    /**
     * 图片上传
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "图片上传V2")
    @RequestMapping(value = "/imageV2", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章"),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public CommonResult<FileResultVo> imageV2(MultipartFile multipart,
                                            @RequestParam(value = "model") String model,
                                            @RequestParam(value = "pid") Integer pid) {
        return CommonResult.success(uploadService.imageV2(multipart, model, pid));
    }

    /**
     * 图片上传OSS - 阿里云
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "图片上传OSS")
    @RequestMapping(value = "/oss/image", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章"),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public CommonResult<FileResultVo> ossImage(MultipartFile multipart,
                                            @RequestParam(value = "model") String model,
                                            @RequestParam(value = "pid") Integer pid, HttpServletRequest request) throws IOException {
        return CommonResult.success(uploadService.ossImage(multipart, model, pid, getMerId(request)));
    }
    
    /**
     * 视频上传OSS
     * @author daigb
     * @date:  2022-03-09 14:35
     */
    @ApiOperation(value = "视频上传OSS")
    @RequestMapping(value = "/oss/video", method = RequestMethod.POST)
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章"),
    	@ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public CommonResult<FileResultVo> ossVideo(MultipartFile multipart,
    		@RequestParam(value = "model") String model,
    		@RequestParam(value = "pid") Integer pid) throws IOException {
    	return CommonResult.success(uploadService.ossVideo(multipart, model, pid));
    }

    /**
     * 文件上传
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "文件上传")
    @RequestMapping(value = "/file", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,news文章"),
            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
    })
    public CommonResult<FileResultVo> file(MultipartFile multipart,
                                           @RequestParam(value = "model") String model,
                                           @RequestParam(value = "pid") Integer pid) throws IOException {
        return CommonResult.success(uploadService.file(multipart, model, pid));
    }

    /**
     * 同步到云
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "同步到云服务")
    @RequestMapping(value = "/async", method = RequestMethod.POST)
    public CommonResult<FileResultVo> async(){
        systemAttachmentService.async();
        return CommonResult.success();

    }

    /**
     * 图片上传zip
     */
//    @ApiOperation(value = "图片上传zip")
//    @RequestMapping(value = "/zip/image", method = RequestMethod.POST)
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "form", name = "files", value = "文件数组", allowMultiple = true, dataType = "__file")
//    })
//    public CommonResult<String> zipImage( @RequestParam("files") MultipartFile[] files) {
//        return CommonResult.success(uploadService.zipImage(files));
//    }
//
//    @ApiOperation(value = "本地上传文件夹// path下为图片文件")
//    @GetMapping(value = "/upload/folder")
//    public CommonResult<String> uploadFolder(String path) {
//        return CommonResult.success(uploadService.uploadFolder(path));
//    }

}



